// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   LittleEndianBinaryFileOps.c
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>

#include "../LittleEndianBinaryFileOps.h"

/**
 *
 * @param file
 * @param asciiCharLE
 */
void readAsciiCharLE(FILE *file, char* asciiCharLE) {
    readByteLE(file, asciiCharLE);
}

/**
 *
 * @param file
 * @param byteLE
 */
void readByteLE(FILE *file, char* byteLE) {
    (*byteLE) = fgetc(file);
}

/**
 *
 * @param file
 * @param shortLE
 */
void readShortLE(FILE *file, short int* shortLE) {
    int i;
    (*shortLE) = 0;
    for (i = 0; i < 2; i++) {
        (*shortLE) = (*shortLE) | (short int) (fgetc(file) << (8 * i));
    }
}

/**
 *
 * @param file
 * @param longLE
 */
void readLongLE(FILE *file, long int* longLE) {
    int i;
    (*longLE) = 0;
    for (i = 0; i < 4; i++) {
        (*longLE) = (*longLE) | (long int) (fgetc(file) << (8 * i));
    }
}

/**
 *
 * @param asciiCharLE
 * @param file
 */
void writeAsciiCharLE(char* asciiCharLE, FILE *file) {
    writeByteLE(asciiCharLE, file);
}

/**
 *
 * @param byteLE
 * @param file
 */
void writeByteLE(char* byteLE, FILE *file) {
    fwrite(byteLE, sizeof (unsigned char), 1, file);
}

/**
 *
 * @param shortLE
 * @param file
 */
void writeShortLE(short int* shortLE, FILE *file) {
    int i;
    char aux;
    short int temp = (*shortLE);
    for (i = 0; i < 2; i++) {
        aux = temp & 0xFF;
        writeByteLE(&aux, file);
        temp = temp >> 8;
    }
}

/**
 * 
 * @param longLE
 * @param file
 */
void writeLongLE(long int* longLE, FILE *file) {
    int i;
    char aux;
    long int temp = (*longLE);
    for (i = 0; i < 4; i++) {
        aux = temp & 0xFF;
        writeByteLE(&aux, file);
        temp = temp >> 8;
    }
}
